package com.qst.crop.demo.controller;

import com.qst.crop.demo.common.Result;
//import com.qst.crop.demo.common.AppVariable;
import com.qst.crop.demo.common.PasswordUtils;
//import com.qst.crop.demo.common.UserSessionUtils;
import com.qst.crop.demo.entity.User;
import com.qst.crop.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 登录
     * @param email
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public Result getUserByName(String email, String password){
        System.out.println("登录功能");

        if(!StringUtils.hasLength(email) || !StringUtils.hasLength(password)){
            return Result.fail(-1, "非法参数");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "没找到该用户，登录失败！");
        }

        if(PasswordUtils.check(password, userinfo.getPassword())){
//            HttpSession session = request.getSession();
//            session.setAttribute(AppVariable.USER_SESSION_KEY, userinfo);

            return Result.success(200, userinfo, "登陆成功");
        }

        return Result.fail(-3, "密码错误");
    }

    /**
     * 注册
     * @param userinfo
     * @return
     */
    @RequestMapping("/reg")
    public Result add(User userinfo){
        System.out.println("注册功能");
        if(userinfo == null || !StringUtils.hasLength(userinfo.getEmail()) || !StringUtils.hasLength(userinfo.getPassword()) || !StringUtils.hasLength(userinfo.getNickname()) || !StringUtils.hasLength(userinfo.getIdentity())){
            return Result.fail(-1, "非法参数");
        }

        // 验证用户是否存在
        if(userService.getUserByEmail(userinfo.getEmail()) != null){
            return Result.fail(-2, "该用户已经存在");
        }

        userinfo.setPassword(PasswordUtils.encrypt(userinfo.getPassword()));
        userinfo.setNickname(userinfo.getNickname());
        userinfo.setIdentity(userinfo.getIdentity());

        userService.add(userinfo);

        return Result.success(200, "注册成功");
    }

    /**
     * 修改密码（已经登录）
     * @param userinfo
     * @param newPassword
     * @return
     */
    @RequestMapping("/updatepassword")
    public Result updatePassword(User userinfo, String newPassword){

        if(userinfo == null || !StringUtils.hasLength(userinfo.getEmail()) || !StringUtils.hasLength(userinfo.getPassword())
        || !StringUtils.hasLength(newPassword)) {
            return Result.fail(-1, "非法参数");
        }

//        // 验证用户是否修改的是自己用户
//        User loginUser = UserSessionUtils.getUser(request);
//        if(loginUser == null){
//            return Result.fail(-2, "请先登录");
//        }

//        if(!userinfo.getEmail().equals(loginUser.getEmail())){
//            return Result.fail(-3, "请修改自己的密码");
//        }

        User loginUser = userService.getUserByEmail(userinfo.getEmail());
        if(PasswordUtils.check(userinfo.getPassword(), loginUser.getPassword())){
            // 修改密码
            userinfo.setPassword(PasswordUtils.encrypt(newPassword));
            userService.updatePassword(userinfo);
        }else {
            return Result.fail(-4, "用户密码输入错误！");
        }

        return Result.success(200, "修改成功");
    }


    /**
     *  忘记密码后，修改密码（未登录）
     * @param email
     * @param newPassword
     * @return
     */
    @RequestMapping("updatepassword2")
    public Result updatePassword2(String email, String newPassword){
        if(!StringUtils.hasLength(newPassword) || !StringUtils.hasLength(email)) {
            return Result.fail(-1, "非法参数");
        }

        // 因为前端已经验证过了，就不再验证了，直接修改密码
        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }else{
            userinfo.setPassword(PasswordUtils.encrypt(newPassword));
            userService.updatePassword(userinfo);
        }

        return Result.success(200, "修改成功");
    }

    /**
     *  退出登录
     * @return
     */
    @RequestMapping("/exit")
    public Result exit(){
//        session.removeAttribute(AppVariable.USER_SESSION_KEY);
        return Result.success(200, "退出成功");
    }


}
